package com.icangya.DAO.impl;

import com.icangya.DAO.IAdminDAO;
import com.icangya.Utils.SQLUtilt;
import com.icangya.domain.Admin;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.hibernate5.HibernateCallback;
import org.springframework.stereotype.Repository;

import java.util.List;

import static com.icangya.Value.StaticValue.PHONE_PAGE_ROW;

@Repository
public class AdminDAOImpl extends  BaseDAO<Admin> implements IAdminDAO {

    Logger logger = LoggerFactory.getLogger(this.getClass());
    @Override
    public int checkAdminByPwd(String name, String passWord) {

        String hql="SELECT count(admin) FROM Admin admin where admin.userName=? and admin.passWord=?";

        Object result= getHibernateTemplate().find(hql,name,passWord).get(0);
        int res=Integer.parseInt(result==null?"":result.toString());
        return res ;
    }


    @Override
    public Admin getAdminByUId(long uid) {
        String hql="FROM Admin admin where admin.uid=?";
        List<Admin> list=(List<Admin>) getHibernateTemplate().find(hql,uid);
        if(list.size()>0)
            return list.get(0);
        return null;
    }

    @Override
    public Admin getAdminById(long id) {
        String hql="FROM Admin admin where admin.id=?";
        List<Admin> list=(List<Admin>) getHibernateTemplate().find(hql,id);
        if(list.size()>0)
            return list.get(0);
        return null;
    }

    @Override
    public Admin getAgentAdminById(long id, int leftNum, int rightNum) {

        String hql="FROM Admin admin where admin.leftNum>? and admin.rightNum<? and admin.id=?";
        List<Admin> list= (List<Admin>) getHibernateTemplate().find(hql,leftNum,rightNum,id);
        if(list.size()>0)
            return list.get(0);
        return null;
    }

    @Override
    public Admin getUserByName(String name) {

        String hql=" FROM Admin admin where admin.userName=? ";
        List<Admin> list= (List<Admin>) getHibernateTemplate().find(hql,name);
        if(list.size()>0)
            return list.get(0);
        return null;
    }

    @Override
    public boolean updateAgentRightNum(int leftNum,long id) {
        //大于等于新增节点的左下标值的右下标值都加2
        String hql="update Admin a set a.rightNum=a.rightNum+2 where a.rightNum>=? and a.id !=?";
//        int result = getHibernateTemplate().execute(new HibernateCallback<Integer>() {
//            @Override
//            public Integer doInHibernate(Session session) throws HibernateException {
//               Query query= session.createQuery(hql);
//                query.setParameter(0,leftNum);
//                int ret=query.executeUpdate();
//                return ret;
//            }
//        });
        return executeUpdate(hql,leftNum,id);
    }

    @Override
    public boolean updateAgentLeftNum(int rightNum,long id) {
        //大于等于新增节点的右下标值的左下标值都加2
        String hql="update Admin a set a.leftNum=a.leftNum+2 where a.leftNum>=? and a.id !=?";

        return executeUpdate(hql,rightNum,id);

    }


    @Override
    public List getLowerAgent(int leftNum, int rightNum,int startPage) {

        String hql="FROM Admin admin where admin.leftNum>"+leftNum+" and admin.rightNum<"+rightNum;
       String count = "SELECT COUNT(admin) " + hql;
        Long total = (Long) getHibernateTemplate().find(count).get(0);
       return  SQLUtilt.getIstance().doPage(getHibernateTemplate(), hql, total, startPage, PHONE_PAGE_ROW);
//        select * from admin where left_num>1 and right_num<6
       // List<Admin> list= (List<Admin>)  getHibernateTemplate().find(hql,leftNum,rightNum);;
//        if(list.size()>0)
//            return list;
//        return null;
    }










    public boolean  executeUpdate(String hql,int num,long id){
        int result = getHibernateTemplate().execute(new HibernateCallback<Integer>() {
            @Override
            public Integer doInHibernate(Session session) throws HibernateException {
                Query query= session.createQuery(hql);
                query.setParameter(0,num);
                query.setParameter(1,id);
                int ret=query.executeUpdate();
                return ret;
            }
        });
        if(result>0)
            return true;
        return false;
    }


}
